from blueapps.account import get_user_model
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render, redirect

from config import APP_CODE
from moments.models import WechatUser, Status


# Create your views here.
def home(request):
    """
    首页
    :param request:
    :return:
    """
    return render(request, 'homepage.html')

def show_user(request):
    """
    获取用户
    :param request:
    :return:
    """
    user_id = request.user.id
    wechat_user = WechatUser.objects.get(user_id =user_id)
    return render(request, 'user.html', {'user': wechat_user})

def show_status(request):
    """
    获取动态
    :param request:
    :return:
    """
    statuses= Status.objects.all()
    return render(request, 'status.html', {'statuses': statuses})

def submit_post(request):
    """
    提交动态
    :param request:
    :return:
    """
    user = WechatUser.objects.get(user = request.user)
    text = request.POST.get('text')
    if text:
        status = Status(user = user, text = text)
        status.save()
        return redirect(f'/stag--{APP_CODE}/status')
    return render(request , 'my_post.html')

def set_super_user(request):
    """
    设置超级用户
    :param request:
    :return:
    """
    user = get_user_model()
    for name in settings.INIT_SUPERUSER:
        user.objects.update_or_create(
            username = name,
            defaults={
                'is_staff': True,
                'is_superuser': True,
                'is_active': True,
            }
        )
    return HttpResponse("Success")
